package c926.xiaoduyun.xiaodu.mservice;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.datamodel.ListSelectedRow;
import kd.bos.entity.datamodel.ListSelectedRowCollection;
import kd.bos.form.CloseCallBack;
import kd.bos.form.FormShowParameter;
import kd.bos.form.ShowType;
import kd.bos.form.control.Toolbar;
import kd.bos.form.control.events.BeforeItemClickEvent;
import kd.bos.form.control.events.ItemClickEvent;
import kd.bos.form.events.ClosedCallBackEvent;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.list.BillList;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.QueryServiceHelper;
import kd.bos.util.StringUtils;
import kd.sdk.plugin.Plugin;

import java.util.*;

/**
 * 消毒方案列表插件
 */
public class XiaoduyunListPlugin extends AbstractFormPlugin implements Plugin {

    @Override
    public void registerListener(EventObject e) {
        //工具栏项注册点击监听
        Toolbar toolbar = this.getView().getControl("toolbarap");
        toolbar.addItemClickListener(this);
    }

    @Override
    public void beforeItemClick(BeforeItemClickEvent evt) {
        super.beforeItemClick(evt);
        String itemKey = evt.getItemKey();
        if ("c926_baritemap".equalsIgnoreCase(itemKey)) {
            //获取所选列表数据
            BillList billList = this.getView().getControl("billlistap");
            ListSelectedRowCollection selectedRow = billList.getSelectedRows();
            if (selectedRow.size() != 1) {
                this.getView().showTipNotification("请选择一条数据进行分配组织！");
                evt.setCancel(true);
                return;
            }
            ListSelectedRow listSelectedRow = selectedRow.get(0);
            Object pkId = listSelectedRow.getPrimaryKeyValue();
            QFilter idQFilter = new QFilter("id", QCP.equals, pkId); // 构造过滤条件
            DynamicObject queryResult = QueryServiceHelper.queryOne("c926_scheme", null, new QFilter[]{idQFilter}); // 查询单条记录
            String status = queryResult.getString("status");
            if (null != status && !status.equals("A") && !status.equals("")) {
                this.getView().showTipNotification("方案为最新版本才可分配");
                evt.setCancel(true);
            }
        }

    }

    @Override
    public void itemClick(ItemClickEvent evt) {
        //获取所选列表数据
        BillList billList = this.getView().getControl("billlistap");
        ListSelectedRowCollection selectedRow = billList.getSelectedRows();
        String itemKey = evt.getItemKey();
        if ("c926_baritemap".equalsIgnoreCase(itemKey)) {
            if (null != selectedRow && selectedRow.size() > 0) {
                List<String> orgList = new ArrayList<>();
                List<String> orgNameList = new ArrayList<>();
                FormShowParameter showParameter = new FormShowParameter();
                Object primaryKeyValue = selectedRow.get(0).getPrimaryKeyValue();
                QFilter idQFilter = new QFilter("id", QCP.equals, primaryKeyValue); // 构造过滤条件
                DynamicObject queryResult = QueryServiceHelper.queryOne("c926_scheme", null, new QFilter[]{idQFilter}); // 查询单条记录
                String orgName = queryResult.getString("c926_orgfield.name");
                String orgNumber = queryResult.getString("c926_orgfield.number");
                if (StringUtils.isNotEmpty(orgName) && StringUtils.isNotEmpty(orgNumber)) {
                    orgList.add(orgNumber);
                    orgNameList.add(orgName);
                }
                showParameter.setFormId("c926_scheme_org");
                showParameter.getOpenStyle().setShowType(ShowType.Modal);
                showParameter.setCustomParam("c926_orgno", orgList);
                showParameter.setCustomParam("parent_id", primaryKeyValue);
                showParameter.setCloseCallBack(new CloseCallBack(this, "distribute_org_callback"));
                this.getView().showForm(showParameter);
            }
        }
        super.itemClick(evt);
    }


    @Override
    public void closedCallBack(ClosedCallBackEvent closedCallBackEvent) {
        super.closedCallBack(closedCallBackEvent);
        String actionId = closedCallBackEvent.getActionId();
        if ("distribute_org_callback".equals(actionId)) {
//            this.getView().showSuccessNotification("分配成功");
            this.getView().invokeOperation("refresh");
        }
    }
}